package cn.ainit.fx.pixiao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ainit.fx.pixiao.entity.FxPiXiao;
import cn.ainit.fx.pixiao.entity.FxPiXiaoItem;
import cn.ainit.sys.common.dao.BusinessBaseDao;
import cn.ainit.sys.utils.UUID32;
@Transactional
@Repository
public class FxPiXiaoDaoImpl extends BusinessBaseDao implements FxPiXiaoDao {

	@Override
	public boolean saveSprk(FxPiXiao fxPiXiao, List<FxPiXiaoItem> fxPiXiaoItems)
			throws Exception {
		this.getSessionFactory().getCurrentSession().save(fxPiXiao);
		for (int i = 0; i < fxPiXiaoItems.size(); i++) {
			fxPiXiaoItems.get(i).setPxItemId(UUID32.newGuid());
			fxPiXiaoItems.get(i).setPxId(fxPiXiao.getPxId());
			this.getSessionFactory().getCurrentSession().save(fxPiXiaoItems.get(i));
		}
		return true;
	}

	@Override
	public boolean updateSprk(FxPiXiao fxPiXiao,
			List<FxPiXiaoItem> fxPiXiaoItems) throws Exception {
		List<FxPiXiaoItem> oldItems = new ArrayList<FxPiXiaoItem>();
		List<FxPiXiaoItem> newItems = new ArrayList<FxPiXiaoItem>();
		//更新操作
		//保存
		//缓存旧的明细IDS
		StringBuffer spItemIdS = new StringBuffer("");
		//遍历明细   判断条件
		for (int i = 0; i < fxPiXiaoItems.size(); i++) {
			//如果有主键，则是原有记录   拼接原有记录id 否则为新记录
			if (fxPiXiaoItems.get(i).getPxItemId() != null && !fxPiXiaoItems.get(i).getPxItemId().equals("")) {
				oldItems.add(fxPiXiaoItems.get(i));
				spItemIdS.append("'"+fxPiXiaoItems.get(i).getPxItemId()+"',");
			}else {
				newItems.add(fxPiXiaoItems.get(i));
			}
		}
		String ids = spItemIdS.toString();
		if (!ids.equals("")) {
			ids = ids.substring(0, ids.length()-1);
		}
		//先删除不存在的
		if (!ids.trim().equals("")) {
			String sql = " delete from yz_sprk_item spi where spi.sp_id = '"+fxPiXiao.getPxId()+"' ";
			sql += " and spi.sp_item_id not in ("+ids+") ";
//			System.out.println(sql);
			this.getJdbcTemplate().execute(sql);
		}
		//更新主表
		this.getSessionFactory().getCurrentSession().update(fxPiXiao);
		//更新明细
		for (int i = 0; i < oldItems.size(); i++) {
			this.getSessionFactory().getCurrentSession().update(oldItems.get(i));
		}
		//更新主表
		for (int i = 0; i < newItems.size(); i++) {
			newItems.get(i).setPxItemId(UUID32.newGuid());
			newItems.get(i).setSpId(fxPiXiao.getPxId());
			this.getSessionFactory().getCurrentSession().save(newItems.get(i));
		}
		return true;
	}

	@Override
	public String execPiXiaoRuku(String pxId, String stockMan) throws Exception {
		String putValue="";
		Connection conn= null;
		CallableStatement cs = null;
		try {
			conn= this.getJdbcTemplate().getDataSource().getConnection();
			conn.setAutoCommit(false);
			String sql = "{Call PK_FX_PX.up_Stock(?,?)}";
			cs = conn.prepareCall(sql);
			cs.setString(1,pxId);
			cs.setString(2,stockMan);
			cs.execute();
			putValue ="success";
		} catch (Exception e) {
			putValue = e.getMessage();
		}finally{
			if(cs != null){
				cs.close();
			}
			if (conn!=null) {
				conn.close();
			}
		}
		return putValue;
	}
	@Override
	public String execChengShou(String pxId, String finMan) throws Exception {
		String putValue="";
		Connection conn= null;
		CallableStatement cs = null;
		try {
			conn= this.getJdbcTemplate().getDataSource().getConnection();
			conn.setAutoCommit(false);
			String sql = "{Call PK_FX_PX.up_FinVerify(?,?)}";
			cs = conn.prepareCall(sql);
			cs.setString(1,pxId);
			cs.setString(2,finMan);
			cs.execute();
			putValue ="success";
		} catch (Exception e) {
			putValue = e.getMessage();
		}finally{
			if(cs != null){
				cs.close();
			}
			if (conn!=null) {
				conn.close();
			}
		}
		return putValue;
	}
}
